﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Brjnk.CoordinateSystem2D.Layers
{
    /// <summary>
    /// Interaction logic for PhotoWindow.xaml
    /// </summary>
    public partial class PhotoWindow : Window
    {
        public PhotoWindow()
        {
            InitializeComponent();
        }

        private double maxSize = 700;
        private IList<IPhoto> photos;
        private int currentPhoto = 0;

        private void nextBtn_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (currentPhoto == photos.Count - 1) return;
            currentPhoto++;
            ShowPhoto(currentPhoto);
        }

        private void previousBtn_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (currentPhoto == 0) return;
            currentPhoto--;
            ShowPhoto(currentPhoto);
        }

        private void closeBtn_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void fullscreenBtn_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Fullscreen();
        }

        private void Fullscreen()
        {
            if (this.WindowState != WindowState.Maximized) this.WindowState = WindowState.Maximized;
            else this.WindowState = WindowState.Normal;
        }

        private void Window_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
        {
            maxSize = Math.Max(100, maxSize + (e.Delta / 120.0) * 48);
            ShowPhoto(currentPhoto);
        }

        private void Image_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                this.DragMove();
                if (e.ClickCount == 2) Fullscreen();
            }

        }

        internal void ShowImages(IList<IPhoto> images)
        {
            this.photos = images;
            ShowPhoto(0);
        }

        private DataTemplate tooltipTemplate;
        private DataTemplateSelector tooltipTemplateSelector;

        internal void SetTooltipTemplate(DataTemplate template, DataTemplateSelector selector)
        {
            this.tooltipTemplate = template;
            this.tooltipTemplateSelector = selector;
            ShowPhoto(currentPhoto);
        }

        private void ShowPhoto(int index)
        {
            if (photos == null || photos.Count <= index || index < 0)
            {
                Reset();
            }
            else
            {
                var photo = photos[index].PhotoFull;
                if (photo == null) { Reset(); return; }
                this.DataContext = photo;
                img.ToolTip = LayerHelper.CreateContent(tooltipTemplate, tooltipTemplateSelector, photo, null);
                double k = Math.Max(photo.Width / maxSize, photo.Width / maxSize);
                var orientation = photos[index].Orientation;

                switch (orientation)
                {
                    case PhotoOrientation.Clockwise90:
                        img.LayoutTransform = new RotateTransform(90);
                        break;

                    case PhotoOrientation.Clockwise270:
                        img.LayoutTransform = new RotateTransform(270);
                        break;

                    default:
                        img.LayoutTransform = null;
                        break;
                }

                if (orientation == PhotoOrientation.Clockwise90 || orientation == PhotoOrientation.Clockwise270)
                {
                    this.Width = photo.Height / k;
                    this.Height = photo.Width / k;
                }
                else
                {
                    this.Width = photo.Width / k;
                    this.Height = photo.Height / k;
                }
            }
        }

        private void Reset()
        {
            this.Width = maxSize;
            this.Height = maxSize;
            this.DataContext = null;
        }
    }
}